<?php

namespace app\business\controller;

use think\Db;
use think\facade\Env;
use app\models\{Order as Orders, Business};
use think\Facade\Config as Configs;

class Main extends Base
{

    public function welcome()
    {
        $data = [];
        $startime = date('Y-m-d') . ' 00:00:00';
        $endtime = date('Y-m-d') . ' 24:00:00';
        $data['order_allcount'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [2, 3, 4]])->count();
        $data['order_todaycount'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [2, 3, 4]])->whereBetweenTime('created', $startime, $endtime)->count();
        $data['order_allfinish'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [4]])->count();
        $data['order_todayfinish'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [4]])->whereBetweenTime('finishtimes', $startime, $endtime)->count();
        $data['order_alltotal'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [4]])->sum('total');
        $data['order_todaytotal'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'status' => [4]])->whereBetweenTime('finishtimes', $startime, $endtime)->sum('total');
        $data['order_alltotals'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'tuikuan' => [1, 2, 3, 4]])->count();
        $data['order_todaytotals'] = (new Orders())->where(['businessid' => $this->request->business['id'], 'shouhou' => 2])->count();
        $data['businessid'] = $this->request->business['id'];
        return view(null, [
            'data' => $data,
        ]);
    }

    public function index()
    {
        $business = $this->request->business;
        $menu = config('bmenu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if ($y['show'] == true) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . config('wechat2.')['app_id'] . "&redirect_uri=https://"
            . Env('APP_API_URL')
            . "/Other/getOpenId?id=" . $business['id'] . '&state=1&response_type=code&scope=snsapi_base#wechat_redirect';
        return view(null, [
            'business' => $business,
            'menu' => $arr,
            'qrcode' => $url
        ]);
    }

    public function jiebang()
    {
        $res = (new Business())->where(['id' => $this->request->business['id']])->update(['openid' => '']);
        if ($res) {
            return json(['status' => 0, 'code' => 0, 'msg' => '解绑成功']);
        }
        return json(['status' => 1, 'code' => 1, 'msg' => '解绑失败，请重试']);
    }

    public function miss()
    {
        return view('404');
    }
}
